define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var Edit = require('modules/fieldEdit').base;
	var dspUtil = require('dspUtil');
	var has_page_channel_adx = pubjs.config('adx_has_pageChannel');
	var dimCount = 3;
	var labels = require('@sys_labels').labels.config;
	var defaultMetric = [];
	var dm = pubjs.config('defaultMetrics');
	if(dm && dm.length)
	{
		for(var i = 0; i < dm.length; i++)
		{
			defaultMetric.push({id: dm[i], name: labels[dm[i]]['text']});
		}
	}

	// 新建报表类型功能
	var Base = Edit.extend({
		init:function(config){
			this.$time = LANG('新建报表_%1', util.date('YmdHis'));
			config = pubjs.conf(config, {
				'class': 'P-reportEdit',
				dim_count: dimCount,
				fields: [
					{text: LANG('名称'), type: 'input', field: 'Name', validate: true , defaultValue: this.$time , max:50},
					{
						text: LANG('广告主'), type: 'custom',
						field: 'Adv',
						uri: 'modules/selectAdver.base', validate: true

					},
					{
						text: LANG('报表类型'), type: 'custom',
						field: 'Date',
						uri: 'report/module/type.base', validate: true
					},
					{
						text: LANG('时间粒度'), type: 'custom',
						field: 'Viewtb',
						uri: 'modules/labelSelect.base',
						config: {
							singleSelect: false,
							data: [
								{id: 0, name: LANG('小时')},
								{id: 1, name: LANG('天')},
								{id: 2, name: LANG('周')},
								{id: 3, name: LANG('月')},
								{id: 4, name: LANG('季')},
								{id: 5, name: LANG('年')}
							]
							,tips: LANG("时间颗粒就是一个报表维度，比如看每小时多少曝光、每天多少曝光")
						}
					},
					{
						text: LANG('维度'), type: 'custom',
						field: 'Keys',
						uri: 'modules/labelSelect.base',
						config:
						{
							maxCount: dimCount,
							data: [
								{id: 'qualification_id', name: LANG('广告主')},
								{id: 'campaign_id', name: LANG('活动')},
								{id: 'package_id', name: LANG('创意包')},
								{id: 'creative_id', name: LANG('创意')},
								{id: 'spot_id', name: LANG('广告位')},
								{id: 'referer_domain_id', name: LANG('媒体域名')},
								{id: 'city_id', name: LANG('城市')},
								{id: 'product_id', name: LANG('产品')},
								{id: 'frequencyslot', name: LANG('频次')},
								{id: 'channel_id', name: LANG('渠道')},
								{id: 'spot_size_id', name: LANG('广告位尺寸')},
								{id: 'page_channel_id', name: LANG('频道分类')},
								{id: 'whisky_id', name: LANG('落地页')}
							]
							,tips: LANG("维度包含落地页时不支持前端和成本指标组")
						}
					},
					{
						text: LANG('指标'), type: 'custom',
						field: 'Metrics',
						uri: 'modules/labelSelect.metricWithPop',
						config:
						{
							defaultValue: defaultMetric
						}
					},
					{
						text: LANG('查看条件'), type: 'custom',
						field: 'Conds',
						uri: 'report/module/condition.base', validate: false
					}
				],
				urlUpdate: 'customDownload/update',
				urlAdd: 'customDownload/add',
				urlInfo: 'customDownload/info',
				nav: 'report/custom',
			});
			this.$saveAs = false;
			this.$edit = false;
			this.Super('init', arguments);
		}
		,reset: function() {
			this.Super('reset', arguments);
			this.cast('userSet', 0);
			this.$time = LANG('新建报表_%1', util.date('YmdHis'));
			this.$.Metrics.setData(util.clone(defaultMetric));
			return this;
		}
		,load: function(id, saveAs)
		{
			this.Super('load', arguments);
			this.$saveAs = false;
			this.$edit = false;
			if(saveAs)
			{
				this.$id = 0;
				this.$saveAs = true;
			}else if(+id){
				this.$edit = true;
			}
			return this;
		}
		,setData: function(data) {
			if(data)
			{
				if(this.$edit || this.$saveAs){
					arguments[0].Date.isEdit = true;
				}else{
					arguments[0].Date.isEdit = false;
				}
				this.Super('setData', arguments);
				var ids = [];
				util.each(data.Adv, function (adv) {
					ids.push(adv.id);
				});
				this.cast('userSet', {ids: ids});
			}
			return this;
		}
		,onRemoveAdver: function(ev){
			console.log(ev.param);
		}
		,dataChange: function(field, v)
		{
			this.Super('dataChange', arguments);
			switch (field)
			{
				case 'Keys':
					if(v && v.indexOf('whisky_id') > -1)
					{
						this.$.Metrics.clearMetricsByWhisky();
					}
					else
					{
						this.$.Metrics.noWhisky();
					}
					break;
				case 'Adv':
					var ids = [];
					util.each(v, function(item){
						if(util.isObject(item)){
							ids.push(item.id);
						}else{
							ids.push(item);
						}
					});

					this.cast('userSet', {ids: ids});
					break;
			}
		}
		,validate: function(data)
		{
			if(this.Super('validate', arguments))
			{

				var AdverId = data['Adv'];
				if(!AdverId || !AdverId.length)
				{
					pubjs.alert('请选择一个广告主');
					return false;
				}

				if(!this.checkLength(data['Metrics'], LANG('指标')))
				{
					return false;
				}

				if(!this.checkLength(data['Keys'], LANG('维度')))
				{
					return false;
				}

				var max = this.getConfig('dim_count');
				if(data.Keys.length > max)
				{
					pubjs.alert(LANG('最多支持%1个维度', max));
					return false;
				}

				var hasPageChannel;
				util.each(data['Keys'], function(key) {
					if(key === 'page_channel_id')
					{
						hasPageChannel = true;
					}
				});

				if(hasPageChannel && data.Conds && data.Conds.grid_cond && data.Conds.grid_cond.length)
				{
					var chooseData;
					util.each(data.Conds.grid_cond[0], function(cond) {
						if(cond.dimId == 'channel_id')
						{
							chooseData = cond.chooseData;
							return false;
						}
					});

					if(chooseData && chooseData.length)
					{
						for(var i = 0; i < chooseData.length; i++)
						{
							var id = +chooseData[i].id;
							if(has_page_channel_adx.indexOf(id) === -1)
							{
								pubjs.alert(LANG('渠道不支持频道分类'));
								return false;
							}
						}
					}
				}
			}
			else
			{
				return false;
			}

			return true;
		}
		,checkLength: function(data, error)
		{
			if(!data || !data.length)
			{
				pubjs.alert('请至少选择一个' + error);
				return false;
			}

			return true;
		}
	});

	exports.base = Base;
});